package org.mspring.mlog.web.task.comment;

import java.util.HashMap;
import java.util.Map;

import org.mspring.mlog.entity.Comment;
import org.mspring.mlog.service.OptionService;
import org.mspring.mlog.utils.PostUrlUtils;
import org.mspring.platform.mail.MailService;
import org.mspring.platform.task.AbstractTask;
import org.mspring.platform.utils.FreemarkerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import freemarker.template.Configuration;

/**
 *
 * @since 2014-04-10 15:00:36
 * @author Gao Youbo
 */
@Component
public class CommentNoticeTask extends AbstractTask {

    private static final String FTL = "/mail/comment_notice.ftl";

    @Autowired
    private OptionService optionService;
    @Autowired
    private MailService mailService;
    @Autowired
    private Configuration configuration;

    @Override
    protected void doTask(Map<Object, Object> paramMap) throws Exception {
        Comment comment = (Comment) paramMap.get("comment");
        if (comment == null) {
            return;
        }
        String siteurl = this.optionService.getOption("siteurl");
        String commentUrl = siteurl
                + PostUrlUtils.getPostUrl(comment.getPost());
        Map<Object, Object> model = new HashMap<>();
        model.put("commentUrl", commentUrl);
        model.put("comment", comment);
        model.put("post", comment.getPost());

        String content = FreemarkerUtils.render(this.configuration, FTL, model);
        String to = comment.getPost().getAuthor().getEmail();
        String personal = comment.getPost().getAuthor().getAlias();
        String subject = this.optionService.getOption("blogname") + " - 文章评论通知";
        this.mailService.sendMail(to, personal, subject, content);
    }

}
